<template>
  <div class="container" style="padding-top: 1.9rem;">
    <app-header :needBack="true" :useSlotTitle="true" ref="header">
      <template slot="title">
        <div class="search-box flex align-items-center justify-content-center">
          <input type="text" name="" v-model="keyword" @change="keywordSearch">
        </div>
      </template>
      <template slot="right-btn">
        <div class="filter-btn" @click="showFilter">
          <i class="iconfont icon-menu"></i>
        </div>
      </template>
    </app-header>
    <filter-view
      v-show="filterOpenState"
      ref="filterView"
      :needTime="true"
      :needSource="true"
      :needCity="true"
      :needCatalog="true"
      :needCompany="true"
      @search="search"
    />
    <nav class="nav" ref="nav">
      <cube-scroll-nav-bar :current="curNavLabel" :labels="navLabelArr" :txts="navTxtsArr" @change="chooseNav">
        <div
          slot-scope="props"
          class="nav-item flex align-items-center justify-content-center"
          :style="{'width': navWidth}">
          {{props.txt}}
        </div>
      </cube-scroll-nav-bar>
    </nav>
    <div class="content" :style="{'height': listViewHeight + 'px'}">
      <!-- 热点推荐 -->
      <hot-recommend class="content-item" :class="{'list-show': curNav === navList[0].name}" ref="HotRecommend" />
      <!-- 宏观资讯 -->
      <macro-infomation class="content-item" :class="{'list-show': curNav === navList[1].name}" ref="MacroInfomation" />
      <!-- 区域要闻 -->
      <area-news class="content-item" :class="{'list-show': curNav === navList[2].name}" ref="AreaNews" />
      <!-- 房企动态 -->
      <house-news class="content-item" :class="{'list-show': curNav === navList[3].name}" ref="HouseNews" />
      <!-- 人物言论 -->
      <people-comment class="content-item" :class="{'list-show': curNav === navList[4].name}" ref="PeopleComment" />
      <!-- 全库查询 -->
      <opinion-search
        class="content-item"
        :class="{'list-show': curNav === navList[5].name}"
        ref="OpinionSearch"
        :keyword="keyword"
      />
    </div>
  </div>
</template>

<script>
import AppHeader from '@/components/common/AppHeader'
import FilterView from '@/components/common/FilterView'
import HotRecommend from './list/HotRecommend'
import MacroInfomation from './list/MacroInfomation'
import AreaNews from './list/AreaNews'
import HouseNews from './list/HouseNews'
import PeopleComment from './list/PeopleComment'
import OpinionSearch from './list/Search'

import { InlineLoading } from 'vux'
import { mapGetters } from 'vuex'
// import * as util from '@/assets/js/util'
const navList = [
  { name: '热点推荐', route: 'HotRecommend', firstLoad: true },
  { name: '宏观资讯', route: 'MacroInfomation', firstLoad: true },
  { name: '区域要闻', route: 'AreaNews', firstLoad: true },
  { name: '房企动态', route: 'HouseNews', firstLoad: true },
  { name: '人物言论', route: 'PeopleComment', firstLoad: true },
  { name: '全库查询', route: 'OpinionSearch', firstLoad: true }
]
const NAV_WIDTH = 1.7
export default {
  data () {
    const navLabelArr = navList.map(item => item.route)
    const navTxtsArr = navList.map(item => item.name)
    return {
      filterOpenState: false,
      navList: navList.slice(0),
      navListWidth: navList.length * NAV_WIDTH + 'rem',
      navWidth: NAV_WIDTH + 'rem',
      navLabelArr,
      navTxtsArr,
      curNavLabel: navLabelArr[0],
      keyword: '',
      filterConditions: {},
      listViewHeight: 0,
      top: 0
    }
  },
  computed: {
    ...mapGetters({
      catalogList: 'catalogList'
    }),
    curNav () {
      const index = this.navLabelArr.indexOf(this.curNavLabel)
      return this.navTxtsArr[index]
    }
  },
  methods: {
    showFilter () {
      this.filterOpenState = true
    },
    hideFilter () {
      this.filterOpenState = false
    },
    search (filterConditions) {
      this.filterConditions = Object.assign(filterConditions)
      this.hideFilter()
      this.searchOpinion()
    },
    keywordSearch () {
      this.searchOpinion()
    },
    searchOpinion () {
      this.chooseNav(this.navLabelArr[this.navLabelArr.length - 1])
      this.$refs.OpinionSearch.search()
      this.$store.commit('setLoading', true)
    },
    chooseNav (cur) {
      this.curNavLabel = cur
      if (this.curNav !== '全库查询') {
        this.$refs.filterView.triggerAddCatelog(this.curNav)
      }
      const item = this.navList[this.navLabelArr.indexOf(cur)]
      if (item.firstLoad) {
        item.firstLoad = false
        if (item.name === '全库查询') {
          this.$refs.filterView.doFilter()
        } else {
          this.$refs[item.route].search()
        }
      }
    }
  },
  mounted () {
    this.curNavLabel = this.navLabelArr[0]
    this.chooseNav(this.curNavLabel)
  },
  activated () {
    this.$nextTick(() => {
      const winHeight = document.documentElement.clientHeight
      const headerHeight = this.$refs.header ? this.$refs.header.$el.clientHeight : 0
      const navHeight = this.$refs.nav ? this.$refs.nav.clientHeight : 0
      this.listViewHeight = winHeight - headerHeight - navHeight
      this.top = headerHeight + navHeight
    })
  },
  components: {
    AppHeader,
    FilterView,
    InlineLoading,
    HotRecommend,
    MacroInfomation,
    AreaNews,
    HouseNews,
    PeopleComment,
    OpinionSearch
  }
}
</script>

<style lang="less" scoped>
  @import '../../assets/less/default.less';
  @height: @header-height;
  .search-box {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: @height;
    line-height: @height;
    font-size: .34rem;
    text-align: center;
    z-index: 1;
    input {
      @input-height: 0.5rem;
      height: @input-height;
      border: none;
      border-radius: 5px;
      width: 4.5rem;
      font-size: 0.3rem;
      line-height: @input-height;
    }
  }
  .nav {
    @navHeight: 1rem;
    width: 100%;
    position: fixed;
    left: 0;
    right: 0;
    top: @height;
    background-color: @white;
    overflow: hidden;
    z-index: 2;
    height: @navHeight;
    .nav-item {
      box-sizing: border-box;
      text-align: center;
      height: @navHeight;
      color: @text-secondary;
      font-size: 0.32rem;
      position: relative;
      &.active {
        color: @red;
        &:after {
          content: "";
          display: block;
          width: 100%;
          height: 5px;
          background-color: @red;
          position: absolute;
          left: 0;
          bottom: 0;
          border-top-left-radius: 5px;
          border-top-right-radius: 5px;
        }
      }
    }
  }
  .article-list {
    background-color: #f4f4f4;
    box-sizing: border-box;
    padding-left: 0.2rem;
    padding-right: 0.2rem;
    padding-top: 0.2rem;
    padding-bottom: 0.2rem;
  }
  .article {
    background-color: @white;
    margin-bottom: 0.2rem;
    border-radius: 5px;
    box-sizing: border-box;
    padding: 0.3rem 0.3rem;
    .article-title {
      color: @text-primary;
      font-size: 0.36rem;
      margin-bottom: 0.2rem;
    }
    .article-summary {
      color: @text-regular;
      font-size: 0.32rem;
      line-height: 0.4rem;
      margin-bottom: 0.2rem;
    }
    .article-bottom {
      color: @text-secondary;
      font-size: 0.28rem;
      .article-source {
        margin-right: 0.4rem;
      }
      .icon-share {
        font-size: 0.32rem;
        color: #54698d;
      }
    }
  }
  .content {
    position: relative;
    height: 0;
    overflow-x: hidden;
    background-color: #f4f4f4;
    z-index: 1;
  }
  .content-item {
    position: absolute;
    width: 100%;
    left: 100%;
    top: 0;
  }
  .nav /deep/ .cube-scroll-nav-bar-item {
    padding: 0;
  }
  .cube-scroll-nav-bar-item_active .nav-item {
    color: @red;
    &:after {
      content: "";
      display: block;
      width: 100%;
      height: 5px;
      background-color: @red;
      position: absolute;
      left: 0;
      bottom: 0;
      border-top-left-radius: 5px;
      border-top-right-radius: 5px;
    }
  }
</style>
